package com.zhenyi.liuxue.core;

import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

import com.alibaba.druid.pool.DruidDataSource;
import com.jfinal.kit.StrKit;
import com.liuxue.common.model.LxCertificateItem;
import com.liuxue.common.model.LxUser;

import cn.hutool.core.codec.Base64;
import cn.hutool.db.Entity;
import cn.hutool.db.SqlRunner;

public class UpdateUtils {

	public void updateCertContent() {
		String superLinkHost = "http://www.superlink.mobi/Uploads/Attachment/";
		String qnhost = "http://res.superlinkplus.com/";
		List<LxCertificateItem> items = LxCertificateItem.dao.findAll();
		for (LxCertificateItem lxCertificateItem : items) {
			String content = lxCertificateItem.getContent();
			if (StrKit.isBlank(content)) {
				continue;
			}
			content = content.replace(qnhost, superLinkHost);
			lxCertificateItem.setContent(content);
			lxCertificateItem.update();
		}
	}

	List<Entity> list = new ArrayList<>();

	public void start() throws SQLException {
		DruidDataSource ds2 = new DruidDataSource();
		ds2.setUrl("jdbc:mysql://123.56.145.42:3306/superlink");
		ds2.setUsername("dev");
		ds2.setPassword("dev");

		String imgHost = "http://res.superlinkplus.com";
		SqlRunner runner = SqlRunner.create(ds2);

		String name = "";
		list = runner.findAll(Entity.create("super_district"));
	}

	private String getName(String eng) {
		if (StrKit.isBlank(eng)) {
			return null;
		}
		for (Entity entity : list) {
			if (eng.equals(entity.getStr("name_en"))) {
				return entity.getStr("name");
			}
		}
		return null;
	}

	public void run() throws Exception {
		List<LxUser> users = LxUser.dao.findAll();
		for (LxUser lxUser : users) {
			String province = lxUser.getProvince();
			province = getName(province);
			String city = lxUser.getCity();
			city = getName(city);
			if (StrKit.notBlank(province)) {
				lxUser.setProvince(province);
			}
			if (StrKit.notBlank(city)) {
				lxUser.setCity(city);
			}
			lxUser.update();
		}
	}

	public static void main(String[] args) throws Exception {
		System.out.println(Base64.encode("姚颂颖🐧"));
	}

}
